package com.nononsenseapps.feeder.db

import com.nononsenseapps.feeder.db.entity.FeedItemTag
import com.nononsenseapps.feeder.db.room.*
import kotlin.jvm.JvmStatic

val SYSTEM_FEED_LIST = listOf(FAVORITE_FEED_ID,RULE_FEED_ID,HOT_FEED_ID,RECYCLE_FEED_ID,STORE_FEED_ID,BACKUP_FEED_ID, MEMO_FEED_ID,
    READ_LATE_FEED_ID)

const val FEED_ITEM_TAG_ROOT_NAME = "_"

const val DEFAULT_PAGE_SIZE = 50

fun buildFeedItemTagRoot()= FeedItemTag(name=FEED_ITEM_TAG_ROOT_NAME, parentTag = "null")

fun Long.isSystemFeed():Boolean = this in SYSTEM_FEED_LIST

fun Long.isFeed():Boolean = this>0 || (this in SYSTEM_FEED_LIST)

fun Long.isUserFeed():Boolean = this>0

fun Long.isAllFeed():Boolean=this== ID_ALL_FEEDS


class DEFAULT{

    object OPEN_TYPE{
        const val PREF_VAL_OPEN_WITH_READER = "0"
        const val PREF_VAL_OPEN_WITH_WEBVIEW = "1"
        const val PREF_VAL_OPEN_WITH_BROWSER = "2"
    }

    object Feed{

    }

    enum class FeedViewListType{
        MINI,NORMAL,PICTURE,EXPANDABLE,MULTI_IMAGE,PICTURE_UNDER,CARD
    }

    object FeedItemType{
        const val FEED_ITEM_TYPE_UN_DEFAULT = 0
        const val FEED_ITEM_TYPE_LOAD = 1

        const val FEED_ITEM_TYPE_NOTES = 4
        const val FEED_ITEM_TYPE_ENDNOTE = 5

    }

    companion object {
        const val SOURCE_HOST="feedin.wsl"
        const val SOURCE_URL= "http://${Companion.SOURCE_HOST}"
    }
}

object ARG{
    const val FEED_ITEM_ID = "feed_item_id"
    const val FEED_ID = "feed_id"
    const val LINK = "link"
    const val IMAGE_URL = "image_url"
    const val TAG = "tag"
    const val SEARCH_TEXT = "search_text"
    const val TITLE = "title"
    const val CUSTOM_TITLE = "customtitle"
    const val AUTHOR = "author"
    const val ENCLOSURE = "enclosure"
    const val TEMPLATE = "template"
    const val FEED_ITEM_RULE_ID = "feed_Item_rule_id"
    const val AD_FLAG = "ad_flag"
    const val FEED_SYNC = "feed_sync"
    const val FEED_SYNC_PERIOD = "feed_sync_period"
}

class UIState{
    object BASE{
        const val START_DATE ="start_date"
        const val END_DATE ="end_date"
    }

    object GLOBAL{
        const val  LAST_UNDO_READ_ITEM_ID="last_undo_read_item_id"
        const val  LAST_OPEN_TAG_TREE_NAME="last_open_tag_tree_name"
    }

    object FeedItemList{
        const val FEED_ITEM_LIST_READ_TYPE_CHANGED =  "feed_item_list_read_type_changed"
        const val FEED_ITEM_LIST_START_DATE =  "feed_item_list_start_date"
        const val FEED_ITEM_LIST_LAST_VIEW_ITEM_ID = "feed_item_list_view_item_id"
        const val FEED_ITEM_LIST_FEATURE_ID = "feed_item_list_feature_id"
        const val FEED_ITEM_FILTER_TAGS =  "feed_item_list_filter_tags"
        const val FEED_ITEM_LIST_ONLY_UNREAD = "feed_item_list_only_unread"
        const val FEED_ITEM_LIST_TYPE = "feed_item_list_type"
        const val FEED_ITEM_BEFORE_LIST_MODEL = "feed_item_before_list_model"
        const val FEED_ITEM_LIST_MODEL = "feed_item_list_model"
        const val FEED_ITEM_LIST_TITLE = "feed_item_list_title"
        const val FEED_ITEM_LIST_SEARCH_TEXT = "feed_item_list_search_text"
        const val FEED_ITEM_LIST_SEARCH_START_DATE = "feed_item_list_search_start_date"
        const val FEED_ITEM_LIST_SEARCH_END_DATE = "feed_item_list_search_end_date"
        const val FEED_ITEM_LIST_REFRESH_ABLE = "feed_item_list_refresh_able"
    }

    object FeedItemReader{
        const val READER_LAST_SCROLL_POSITION = "reader_last_scroll_position"
    }
}

class SettingProp{

    object BASE{
        const val PREF_THEME = "pref_theme"
        /**
         * Boolean indicating whether we performed the (one-time) welcome flow.
         */
        const val PREF_WELCOME_DONE = "pref_welcome_done"
    }

    object DATA{
        const val PREF_ITEM_HOT_TAGS="pref_item_hot_tags"

        /**
         * These indicate which fragment to open by default
         */
        const val PREF_LAST_FEED_TAG = "pref_last_feed_tag"
        const val PREF_LAST_FEED_ID = "pref_last_feed_id"

        const val PREF_HOT_FEEDS_LIST_IDS = "pref_hot_feed_list_ids"
    }

    object UI{
        /**
         * Image settings
         */
        const val PREF_IMG_ONLY_WIFI = "pref_img_only_wifi"

        const val PREF_IMG_USE_COIL_LOAD = "pref_img_use_coil_load"

        /**
         * Boolean indicating if only unread items should be shown
         */
        const val PREF_SHOW_ONLY_UNREAD = "pref_show_only_unread"

        const val PREF_READ_TEXT_SIZE = "pref_read_text_size"

        const val PREF_JAVASCRIPT_ENABLED = "pref_javascript_enabled"

        const val PREF_BACKGROUND_COLOR_ON_DAY = "pref_background_color_on_day"

        const val PREF_BACKGROUND_COLOR_ON_NIGHT = "pref_background_color_on_night"

        const val PREF_FEED_VIEW_LIST_TYPE="pref_feed_view_list_type"

        const val PREF_GRAY_MODEL="pref_gray_model"

        const val PREF_ITEM_LIST_ORDER="pref_item_list_order"
    }

    object BACKGROUND{
        /**
         * Sync settings
         */
        const val PREF_SYNC_ONLY_CHARGING = "pref_sync_only_charging"
        const val PREF_SYNC_ONLY_WIFI = "pref_sync_only_wifi"
        const val PREF_SYNC_FREQ = "pref_sync_freq"
        const val PREF_SYNC_ON_RESUME = "pref_sync_on_resume"

        /**
         * Database settings
         */
        const val PREF_MAX_ITEM_COUNT_PER_FEED = "pref_max_item_count_per_feed"
    }

}


/** 模板
const val tableName=""
const val COL_ID = BASE.COL_ID
@JvmStatic
fun tableColumn(column:String):String= "$tableName.$column"
@JvmStatic
fun tableColumnAS(column:String):String= "$tableName.$column as $column"
@JvmStatic
fun tableColumnAS(column:String,newColumn:String):String= "$tableName.$column as $newColumn"
 */

object BASE{
    const val COL_ID = "id"
    const val COL_NAME = "name"
    const val COL_TITLE = "title"
    const val COL_FEED_ID = "feed_id"
    const val COL_CREATE_DATE = "create_date"
    const val COL_LINK = "link"
    const val COL_ATTENTION_LEVEL="attention_level"
    const val COL_BODY_FORMAT_TYPE="body_format_type"
    const val COL_IMAGE_URL = "image_url"
    const val COL_CLICK_COUNT = "click_count"
    const val COL_FEED_ITEM_ID = "feed_item_id"
    const val COL_HOT = "hot"
    const val COL_ABLE = "able"
    const val COL_FULL_TEXT_SYNC = "full_text_sync"
    const val COL_PRE_DOWNLOAD = "pre_download"
    const val COL_TYPE = "type"
    const val COL_IMAGE_PRE_DOWNLOAD = "image_pre_download"
    const val COL_TAG = "tag"
}

object FEED  {
    const val tableName="feeds"
    const val COL_ID = BASE.COL_ID
    const val COL_TITLE = "title"
    const val COL_CUSTOM_TITLE = "custom_title"
    const val COL_URL = "url"
    const val COL_TAG = BASE.COL_TAG
    const val COL_NOTIFY = "notify"
    const val COL_IMAGE_URL = BASE.COL_IMAGE_URL
    const val COL_LINK = BASE.COL_LINK
    const val COL_LAST_SYNC_TIME = "last_sync"
    const val COL_LAST_SUCCESS_SYNC_TIME = "last_success_sync"
    const val COL_RESPONSE_HASH = "response_hash"
    const val COL_FEED_SET_OPEN_TYPE = "feed_set_open_type"
    const val COL_FEED_SET_OPEN_TYPE_TREE_NAME = "feed_open_type_tree_name"

    const val COL_FEED_SET_SYNC_PERIOD = "feed_set_sync_period"
    const val COL_FEED_SET_SYNC_ABLE = "feed_set_sync_able"
    const val COL_FEED_LAST_SYNC_SUCCESS = "feed_last_sync_success"
    const val COL_ATTENTION_LEVEL=BASE.COL_ATTENTION_LEVEL
    const val COL_FEED_STATE_NET_ERR = "feed_state_net_err"
    const val COL_FEED_SYNC_NEW_ITEM_COUNT="new_item_count"
    const val COL_RULE_ABLE = "rule_able"
    const val COL_BACKUP = "backup"
    const val COL_OVERWRITE = "overwrite"
    const val COL_FEED_ITEM_TYPE = "item_type"
    const val COL_FEED_ITEM_BODY_FORMAT_TYPE = BASE.COL_BODY_FORMAT_TYPE
    const val COL_FEED_ITEM_BODY_REFORMAT = "reformat_body"
    const val COL_FEED_ITEM_READ_LIST_ALL = "read_list_all"
    const val COL_FULL_TEXT_SYNC = BASE.COL_FULL_TEXT_SYNC
    const val COL_PRE_DOWNLOAD = BASE.COL_PRE_DOWNLOAD
    const val COL_IMAGE_PRE_DOWNLOAD = BASE.COL_IMAGE_PRE_DOWNLOAD

    const val COL_MUTABLE_SERVER_FEED = "mutable_server_feed"

    const val COL_LINK_CONF = "link_conf"
    const val COL_XPATH_CONF = "xpath_conf"

    @JvmStatic
    fun tableColumn(column:String):String= "$tableName.$column"
    @JvmStatic
    fun tableColumnAS(column:String):String= "$tableName.$column as $column"
    @JvmStatic
    fun tableColumnAS(column:String,newColumn:String):String= "$tableName.$column as $newColumn"
}

object FEED_ITEM{
    const val tableName="feed_items"
    const val COL_ID = BASE.COL_ID
    const val COL_TYPE = "type"
    const val COL_TITLE = BASE.COL_TITLE
    const val COL_IMAGE_URL = BASE.COL_IMAGE_URL
    const val COL_IMAGE_COUNT = "image_count"
    const val COL_ITEM_TAGS = "item_tags"
    const val COL_ITEM_FEATURE="item_feature"
    const val COL_READ_HISTORY = "read_history"
    const val COL_GUID = "guid"
    const val COL_DESCRIPTION = "description"
    const val COL_PLAIN_TITLE = "plain_title"
    const val COL_PLAIN_SNIPPET = "plain_snippet"
    const val COL_ENCLOSURE_LINK = "enclosure_link"
    const val COL_AUTHOR = "author"
    const val COL_PUB_DATE = "pub_date"
    const val COL_UNREAD = "unread"
    const val COL_NOTIFIED = "notified"
    const val COL_CREATE_DATE = BASE.COL_CREATE_DATE
    const val COL_FEED_ID = BASE.COL_FEED_ID
    const val COL_LINK = BASE.COL_LINK
    const val COL_RULE_CHECKED = "rule_checked"
    const val COL_WORD_COUNT = "word_count"
    const val COL_ATTENTION_LEVEL = BASE.COL_ATTENTION_LEVEL
    const val COL_BODY_FORMAT_TYPE = BASE.COL_BODY_FORMAT_TYPE
    const val COL_FULL_TEXT_SYNC = BASE.COL_FULL_TEXT_SYNC
    //const val COL_PRE_LOAD_STATE = "pre_load_state"
    @JvmStatic
    fun tableColumn(column:String):String= "$tableName.$column"
    @JvmStatic
    fun tableColumnAS(column:String):String= "$tableName.$column as $column"
    @JvmStatic
    fun tableColumnAS(column:String,newColumn:String):String= "$tableName.$column as $newColumn"
}

object FEED_ITEM_FEED{
    const val COL_FEEDTITLE = "feed_title"
    const val COL_FEEDCUSTOMTITLE = "feed_customtitle"
    const val COL_FEEDURL = "feed_url"
}

object FEED_ITEM_TAG{
    const val tableName="feed_item_tag"
    const val COL_ID = BASE.COL_ID
    const val COL_NAME = BASE.COL_NAME
    const val COL_ATTENTION_LEVEL=BASE.COL_ATTENTION_LEVEL
    const val COL_PARENT_TAG = "parent_tag"
    const val COL_CLICK_COUNT = BASE.COL_CLICK_COUNT
    const val COL_HOT = BASE.COL_HOT

    @JvmStatic
    fun tableColumn(column:String):String= "${FEED_ITEM.tableName}.$column"
    @JvmStatic
    fun tableColumnAS(column:String):String= "${FEED_ITEM.tableName}.$column as $column"
    @JvmStatic
    fun tableColumnAS(column:String,newColumn:String):String= "${FEED_ITEM.tableName}.$column as $newColumn"
}


object FEED_SYNC_ITEM{
    const val tableName="feed_sync_item"
    const val COL_ID = BASE.COL_ID
    const val COL_FEEDID = BASE.COL_FEED_ID
    const val COL_SYNC_SUCCESS = "sync_success"
    const val COL_FEED_SYNC_NEW_ITEM_COUNT="new_item_count"
    const val COL_FEED_SYNC_TIME_INTERVAL="sync_time_interval"
    const val COL_CREATEDATE = BASE.COL_CREATE_DATE
    @JvmStatic
    fun tableColumn(column:String):String= "$tableName.$column"
    @JvmStatic
    fun tableColumnAS(column:String):String= "$tableName.$column as $column"
    @JvmStatic
    fun tableColumnAS(column:String,newColumn:String):String= "$tableName.$column as $newColumn"
}


object FEED_ITEMS_FEATURE{
    const val tableName="feed_item_feature"
    const val COL_ID = BASE.COL_ID
    const val COL_TITLE = BASE.COL_TITLE
    const val COL_CREATEDATE = BASE.COL_CREATE_DATE
    @JvmStatic
    fun tableColumn(column:String):String= "$tableName.$column"
    @JvmStatic
    fun tableColumnAS(column:String):String= "$tableName.$column as $column"
    @JvmStatic
    fun tableColumnAS(column:String,newColumn:String):String= "$tableName.$column as $newColumn"
}

object FEED_RULE{
    const val tableName="feed_rule"
    const val COL_ID = BASE.COL_ID
    const val COL_TITLE = BASE.COL_TITLE
    const val COL_FEEDID = BASE.COL_FEED_ID
    const val COL_AD_CHECK = "ad_check"
    const val COL_STYLE = "style" //
    const val COL_RULE_VALUE = "rule_value"
    const val COL_ABLE = BASE.COL_ABLE
    const val COL_ACTION = "rule_action"
    const val COL_ACTION_FEEDID = "rule_action_feed_id"
    const val COL_ACTION_REPLACE_VALUE = "replace_value"
    const val COL_RULE_VALUE_IS_REGEX_PATTERN = "regex_pattern"
    @JvmStatic
    fun tableColumn(column:String):String= "$tableName.$column"
    @JvmStatic
    fun tableColumnAS(column:String):String= "$tableName.$column as $column"
    @JvmStatic
    fun tableColumnAS(column:String,newColumn:String):String= "$tableName.$column as $newColumn"
}


object FEED_ITEM_PRELOAD_ITEM{
    const val tableName="feed_item_preload_item"
    const val COL_ID = BASE.COL_ID
    const val COL_FEED_ITEM_ID = BASE.COL_FEED_ITEM_ID
    const val COL_LOAD_STATE = "state"
    const val COL_RRELOAD_URL = "preload_url"
    const val COL_LOCAL_URI = "local_uri"
    const val COL_IS_ENCLOSURE = "is_enclosure"
    const val COL_SIZE = "size"
    const val COL_CREATE_DATE = BASE.COL_CREATE_DATE
    const val COL_TYPE = BASE.COL_TYPE
    @JvmStatic
    fun tableColumn(column:String):String= "$tableName.$column"
    @JvmStatic
    fun tableColumnAS(column:String):String= "$tableName.$column as $column"
    @JvmStatic
    fun tableColumnAS(column:String,newColumn:String):String= "$tableName.$column as $newColumn"
}


